#!/bin/bash

source ../cluster_IP.conf

function config_peer0_IP() {
    i=1
    for node in ${FABRIC_PEER0_IP[@]}
    do
        IP_in_hosts=$(grep peer0.org${i}.demo.com /etc/hosts | awk '{print $1}')
        if [ "$IP_in_hosts" == "" ]; then
            echo "$node peer0.org${i}.demo.com" | sudo tee -a /etc/hosts
        elif [ $IP_in_hosts != $node ]; then
            sudo sed -i "s/.*peer0.org${i}.demo.com/$node peer0.org${i}.demo.com/g" /etc/hosts
        fi
        let i=$i+1
    done
}

function config_peer1_IP() {
    i=1
    for node in ${FABRIC_PEER1_IP[@]}
    do
        IP_in_hosts=$(grep peer1.org${i}.demo.com /etc/hosts | awk '{print $1}')
        if [ "$IP_in_hosts" == "" ]; then
            echo "$node peer1.org${i}.demo.com" | sudo tee -a /etc/hosts
        elif [ $IP_in_hosts != $node ]; then
            sudo sed -i "s/.*peer1.org${i}.demo.com/$node peer1.org${i}.demo.com/g" /etc/hosts
        fi
        let i=$i+1
    done
}

function config_orderer_IP() {
    i=1
    for node in ${FABRIC_ORDERER_IP[@]}
    do
        IP_in_hosts=$(grep orderer${i}.demo.com /etc/hosts | awk '{print $1}')
        if [ "$IP_in_hosts" == "" ]; then
            echo "$node orderer${i}.demo.com" | sudo tee -a /etc/hosts
        elif [ $IP_in_hosts != $node ]; then
            sudo sed -i "s/.*orderer${i}.demo.com/$node orderer${i}.demo.com/g" /etc/hosts
        fi
        let i=$i+1
    done
}

config_peer0_IP
config_peer1_IP

config_orderer_IP
